--#include "data\config\vip\VipConcurrentConfig.lua" once

local ConcVipWay =
{
	AddByYuanBao 	= 1, 		--购买
	AddByItem		= 2, 		--道具
}

--登陆
function OnConcVipLogin( sysarg )
	--print("OnConcVipLogin...")
	CheckConcVipValidOrExpire( sysarg, enConcVip1)		--检测青铜卡
	CheckConcVipValidOrExpire( sysarg, enConcVip2)		--检测白银卡
	CheckConcVipValidOrExpire( sysarg, enConcVip3)		--检测黄金卡
	CheckConcVipValidOrExpire( sysarg, enConcVip4)		--检测白金卡
	SendConcVipDailyGiftData( sysarg )
end

--购买钻石贵族（并行的VIP）
function BuyConcVip( sysarg, vipType )
	--print("  BuyConcVip, vipType="..vipType)
	local vip = VipConcCfg.Vips[vipType]
	if not vip then
		return
	end

	if not vip.buyNeedYB or vip.buyNeedYB <= 0 then
		return
	end

	if not CheckConsumeMoney( sysarg, mtYuanbao, vip.buyNeedYB, 1, false ) then
		return
	end

	if not DoConsumeMoney( sysarg, mtYuanbao, vip.buyNeedYB, 1, GameLog.clBuyConcVipFee, Lang.ScriptTips.vipConcLog01, false ) then
		return
	end

	local vipName = vip.vipName
	if Actor.isConcurrentVip( sysarg, vipType ) then
		Actor.sendTipmsg(sysarg, string.format( Lang.ScriptTips.vipConc011, vipName ), ttFlyTip)
		return
	end

	--print("  BuyConcVip, buyLastDay="..vip.buyLastDay)
	if DoBuyConcVip( sysarg, vip ) then
		local actorId 	= Actor.getActorId(sysarg)
		local actorName = Actor.getName(sysarg)

		if vip.buyVipGift then		--有才发
			local title 	= Lang.ScriptTips.MailTitle1
			local content 	= string.format( Lang.ScriptTips.MailCont1, GetConcVipName( vip.vipType ) )
			Awards.GiveToMailById( actorId, vip.buyVipGift, title, content, GameLog.clSendConcVipBuyGift, Lang.ScriptTips.vipConcLog07 )
		end

		SendActorAllDailyOpTimes( sysarg )		--特权变化，通知客户端
		System.sendCommonLog( GameLog.clAddConcVip, actorId, actorName, vip.vipType, needYb, ConcVipWay.AddByYuanBao, "", "", 
			Lang.ScriptTips.vipConcLog01 )
	end
end

--续费钻石贵族
function RenewalConcVip( sysarg, vipType )
	--print("  RenewalConcVip, vipType="..vipType)
	local vip = VipConcCfg.Vips[vipType]
	if not vip then
		return
	end

	if not vip.renewalNeedYB or vip.renewalNeedYB <= 0 then
		return
	end 

	if not CheckConsumeMoney( sysarg, mtYuanbao, vip.renewalNeedYB, 1, false ) then
		return
	end

	if not DoConsumeMoney( sysarg, mtYuanbao, vip.renewalNeedYB, 1, GameLog.clBuyConcVipFee, Lang.ScriptTips.vipConcLog02, false ) then
		return
	end


	local vipName = vip.vipName
	if not Actor.isConcurrentVip( sysarg, vipType ) then
		Actor.sendTipmsg(sysarg, string.format( Lang.ScriptTips.vipConc012, vipName ), ttFlyTip)
		return
	end

	--print("  RenewalConcVip, renewalLastDay="..vip.renewalLastDay)
	if DoRenewalConcVip( sysarg, vip ) then
		local actorId 	= Actor.getActorId(sysarg)
		local actorName = Actor.getName(sysarg)

		if vip.buyVipGift then		--有才发
			local title 	= Lang.ScriptTips.MailTitle1
			local content 	= string.format( Lang.ScriptTips.MailCont1, GetConcVipName( vip.vipType ) )
			Awards.GiveToMailById( actorId, vip.buyVipGift, title, content, GameLog.clSendConcVipBuyGift, Lang.ScriptTips.vipConcLog07 )
		end

		System.sendCommonLog( GameLog.clAddConcVip, actorId, actorName, vip.vipType, needYb, ConcVipWay.AddByYuanBao, "", "", 
			Lang.ScriptTips.vipConcLog02 )
	end
end

--[[
团购
]]
function BuyAllConcVip( sysarg )
	if not VipConcCfg.allBuyNeedYB or VipConcCfg.allBuyNeedYB <= 0 then
		return
	end 

	if not CheckConsumeMoney( sysarg, mtYuanbao, VipConcCfg.allBuyNeedYB, 1, false ) then
		return
	end

	if not DoConsumeMoney( sysarg, mtYuanbao, VipConcCfg.allBuyNeedYB, 1, GameLog.clBuyConcVipFee, Lang.ScriptTips.vipConcLog03, false ) then
		return
	end

	local actorId 	= Actor.getActorId( sysarg )
	local actorName = Actor.getName( sysarg )
	local vipTypes = { enConcVip1, enConcVip2, enConcVip3, enConcVip4 }
	for i,vipType in ipairs( vipTypes ) do
		local vip = VipConcCfg.Vips[vipType]
		if vip then
			local addVipTime = vip.buyLastDay*24*3600
			Actor.addConcurrentVip( sysarg, vip.vipType, true, addVipTime)

			if vip.buyVipGift then		--有才发
				local title 	= Lang.ScriptTips.MailTitle1
				local content 	= string.format( Lang.ScriptTips.MailCont1, GetConcVipName( vip.vipType ) )
				Awards.GiveToMailById(actorId, vip.buyVipGift, title, content, GameLog.clSendConcVipBuyGift, Lang.ScriptTips.vipConcLog07 )
			end
		end
	end

	SendConcVipDailyGiftData( sysarg )
	SendConcVipPrivNum( sysarg )

	SendActorAllDailyOpTimes( sysarg )		--特权变化，通知客户端
	
	local msg = string.format( Lang.ScriptTips.vipConc023, Actor.getName(sysarg) )
	System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow )
end

--领取并行VIP（特效）每日礼包
function GetConcVipGift( sysarg, vipType )
	--print("  GetConcVipGift, vipType="..vipType)
	local vip = VipConcCfg.Vips[vipType]
	if not vip then
		return
	end

	if not Actor.isConcurrentVip( sysarg, vipType ) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.vipConc015, GetConcVipName(vipType)), ttFlyTip)
		return
	end

	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.concVipGift then
		sVar.concVipGift = {}
	end

	local num = sVar.concVipGift[vipType] or 0
	if num >= 1 then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.vipConc014, GetConcVipName(vipType)), ttFlyTip)
		return
	end
	
	if not IsBagGridEnough(sysarg, vip.dailyVipGift) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.vipConc018, ttFlyTip)
		return
	end

	sVar.concVipGift[vipType] = 1
	GiveCommonAward(sysarg, vip.dailyVipGift, GameLog.clGetConcVipGetGift, Lang.ScriptTips.vipConc017)
	SendConcVipDailyGiftData( sysarg )
end


function CheckConcVipValidOrExpire( sysarg, vipType)
	if Actor.isConcurrentVip( sysarg, vipType ) then   --是VIP
		ProcConcVipValid( sysarg, vipType )
	else
		ProcConcVipExpire( sysarg, vipType )
	end
end

--
function ProcConcVipValid( sysarg, vipType )
	--print("ProcConcVipValid, vipType="..vipType)
	local vip = VipConcCfg.Vips[vipType]
	if not vip then
		return
	end

	local myJob = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
	if vip.jobBuff then		--按职业区分的buff
		local buffId = vip.jobBuff[myJob] or 0
		if not Actor.hasBuffById(sysarg, buffId) then
			Actor.addBuffById(sysarg, buffId)
		end
	end

	--如果是vip，则加buff
	for i, buffId in ipairs(vip.vipBuff) do
		if not Actor.hasBuffById(sysarg, buffId) then
			Actor.addBuffById(sysarg, buffId)
		end
	end

	if vip.titleId and vip.titleId > 0 then
		if not Actor.hasTopTitle( sysarg, vip.titleId ) then
			Actor.addTopTitle( sysarg, vip.titleId )
		end
	end
end

--判断VIP是否过期
function ProcConcVipExpire( sysarg, vipType )
	--print("ProcConcVipExpire, vipType="..vipType)
	local vip = VipConcCfg.Vips[vipType]
	if not vip then
		return
	end

	--如果不是vip，则减buff
	for i, buffId in ipairs(vip.jobBuff) do
		if Actor.hasBuffById(sysarg, buffId) then
			Actor.delBuffById(sysarg, buffId)
		end
	end

	--如果不是vip，则减buff
	for i, buffId in ipairs(vip.vipBuff) do
		if Actor.hasBuffById(sysarg, buffId) then
			Actor.delBuffById(sysarg, buffId)
		end
	end

	if vip.titleId and vip.titleId > 0 then
		if Actor.hasTopTitle( sysarg, vip.titleId ) then
			Actor.delTopTitle( sysarg, vip.titleId )
		end
	end
end

--VIP生效
function OnConcVipValid( sysarg, vipType )
	local vip = VipConcCfg.Vips[vipType]
	if not vip then
		return
	end
	ProcConcVipValid( sysarg, vipType )
	SendConcVipPrivNum( sysarg )
end

--VIP是否过期
function OnConcVipExpire( sysarg, vipType )
	--print("OnConcVipExpire, vipType="..vipType)
	local vip = VipConcCfg.Vips[vipType]
	if not vip then
		return
	end

	ProcConcVipExpire( sysarg, vipType )
	SendConcVipPrivNum( sysarg )
	local msg = string.format(Lang.ScriptTips.vipConc016, GetConcVipName(vipType))
	Actor.sendTipmsg(sysarg, msg, ttTipmsgWindow+ttChatWindow)

	local actorId 	= Actor.getActorId( sysarg )
	local actorName = Actor.getName( sysarg )
	System.sendCommonLog( GameLog.clDelConcVip, actorId, actorName, vipType, 0, 0, "", "", Lang.ScriptTips.vipConcLog06)
end

--特权每日处理
function OnConcVipDailyProc( sysarg )
	--print("OnConcVipDailyProc...")
	local sVar = Actor.getStaticVar(sysarg)
	sVar.concVipGift = nil
	SendConcVipDailyGiftData( sysarg )
end


--[[购买特权
addVipHour:增加的VIP时间，单位（小时）
]]
function DoBuyConcVip( sysarg, vip, addVipHour)
	local addVipTime = 0
	if addVipHour then
		addVipTime = addVipHour*3600				--秒--测试的
	else
		addVipTime = vip.buyLastDay*24*3600			--秒
	end
	--print("DoBuyConcVip, vipType="..vip.vipType..", addVipTime="..addVipTime)
	if Actor.addConcurrentVip( sysarg, vip.vipType, true, addVipTime) then
		--Actor.playScrEffect(sysarg, vip.effectId, vip.effectTime, false)	
		SendConcVipDailyGiftData( sysarg )
		SendConcVipPrivNum( sysarg )
		local msg = string.format( Lang.ScriptTips.vipConc019, Actor.getName(sysarg), vip.vipName )
		System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow )
		return true
	end
	return false
end

--续费特权
function DoRenewalConcVip( sysarg, vip, addVipHour )
	local addVipTime = 0
	if addVipHour then
		addVipTime = addVipHour*3600				--秒
	else
		addVipTime = vip.buyLastDay*24*3600			--秒
	end

	if Actor.addConcurrentVip( sysarg, vip.vipType, true, addVipTime) then
		--Actor.playScrEffect(sysarg, vip.effectId, vip.effectTime, false)
		SendConcVipDailyGiftData( sysarg )
		SendConcVipPrivNum( sysarg )
		local msg = string.format( Lang.ScriptTips.vipConc020, Actor.getName(sysarg), vip.vipName )
		System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow)
		return true
	end
	return fasle
end

function SendConcVipDailyGiftData( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.concVipGift then
		sVar.concVipGift = {}
	end

	local netPack = DataPack.allocPacket(sysarg, enVipSystem, sSendConcVipGiftData)
	if netPack then
		local num1 = sVar.concVipGift[enConcVip1] or 0
		local num2 = sVar.concVipGift[enConcVip2] or 0
		local num3 = sVar.concVipGift[enConcVip3] or 0
		local num4 = sVar.concVipGift[enConcVip4] or 0
	    DataPack.writeChar(netPack, num1)
	    DataPack.writeChar(netPack, num2)
		DataPack.writeChar(netPack, num3) 
		DataPack.writeChar(netPack, num4) 
	    DataPack.flush(netPack)
	    --print("SendConcVipDailyGiftData, num1="..num1..", num2="..num2..",num3="..num3..", num4="..num4)
	end
end

--发送VIP权限数量
function SendConcVipPrivNum( sysarg )
--[[
	sendItemUseCount(sysarg, 1)
	sendItemUseCount(sysarg, 2)
	sendItemUseCount(sysarg, 3)
	sendItemUseCount(sysarg, 4)
]]
end

function GetConcVipName( vipType )
	if vipType == enConcVip1 then
		return Lang.ScriptTips.vipConc001
	elseif vipType == enConcVip2 then
		return Lang.ScriptTips.vipConc002
	elseif vipType == enConcVip3 then
		return Lang.ScriptTips.vipConc003
	elseif vipType == enConcVip4 then
		return Lang.ScriptTips.vipConc004
	end
end

--[[是否某个特权
vipTypesIn:
	nil，	任意特权类型
	非nil，	某个特权类型
]]
function IsConcVip( sysarg, vipTypesIn )
	if vipTypesIn then
		vipTypes = vipTypesIn
	else
		vipTypes = { enConcVip1, enConcVip2, enConcVip3, enConcVip4 } 
	end
	for i, vipType in ipairs(vipTypes) do
		if Actor.isConcurrentVip( sysarg, vipType ) then
			return true
		end
	end
	return false
end